import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sml_policies/locator/locator.dart';
import 'package:sml_policies/locator/sml_policies.dart';
import 'package:sml_policies/ui/webview/webview_common.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'exit_confirm_dialog.dart';

class AgreementViewDialog extends Dialog {
  AgreementViewDialog({
    Key? key,
    this.confirm,
    this.cancel
  }) : super(key: key);

  Function? confirm;
  Function? cancel;

  @override
  Widget build(BuildContext context) {
    return AgreementViewContent(
      confirm: confirm,
      cancel: cancel,
    );
  }

  Future show(BuildContext context) async {
    await showDialog(context: context, barrierDismissible: false, builder: (BuildContext context) {
      return AgreementViewDialog();
    });
  }

}

class AgreementViewContent extends StatefulWidget {
  AgreementViewContent({
    Key? key,
    this.confirm,
    this.cancel
  }) : super(key: key);

  Function? confirm;
  Function? cancel;

  @override
  State<AgreementViewContent> createState() => _AgreementViewContentState();
}

class _AgreementViewContentState extends State<AgreementViewContent> {

  bool ableToConfirm = false;
  SmlPoliciesConfig smlPoliciesConfig = locator<SmlPoliciesConfig>();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: WillPopScope(
        child: Container(
          margin: const EdgeInsets.only(top: 40, left: 8, right: 8, bottom: 40),
          decoration: const BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(24.0)),
            color: Colors.white,
          ),
          child: Column(
            children: [
              const SizedBox(height: 20),
              Expanded(
                child: CommonWebView(
                  url: smlPoliciesConfig.privacyUrl ?? '',
                  onScrollToBottom: () {
                    debugPrint('onScrollToBottom ====');
                    setState(() {
                      ableToConfirm = true;
                    });
                  },
                )
              ),
              ableToConfirm ? const SizedBox() : Container(
                margin: const EdgeInsets.only(left: 15, right: 15, top: 10, bottom: 10),
                child: Text("${smlPoliciesConfig.languageMap['slideBottomHint']}", style: const TextStyle(color: Colors.red)),
              ),
              Container(
                margin: const EdgeInsets.symmetric(vertical: 10),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    const SizedBox(width: 10),
                    rejectBtn(context),
                    const SizedBox(width: 10),
                    Container(),
                    const SizedBox(width: 10),
                    sureBtn(context),
                    const SizedBox(width: 10),
                  ],
                ),
              ),
              const SizedBox(width: 10),
            ],
          ),
        ),
        onWillPop: () async {
          return false;
        }
      ),
    );
  }

  Widget sureBtn(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () async {
        confirm();
      },
      child: Container(
        height: 50,
        width: 150,
        decoration: BoxDecoration(
          borderRadius: const BorderRadius.all(Radius.circular(90.0)),
          color: ableToConfirm ? smlPoliciesConfig.mainColor : Colors.grey.withOpacity(0.4),
        ),
        child: Center(
          child: Text(smlPoliciesConfig.languageMap['agree'].toString(),
              style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold,
                  color: Colors.white)
          ),
        ),
      ),
    );
  }

  Widget rejectBtn(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: (){
        ExitConfirmDialog(
          onConfirm: () {
            smlPoliciesConfig.logEvent("loginphone-permission-exit");
            SystemNavigator.pop();
          },
        ).show(context);
      },
      child: Container(
        height: 50,
        width: 150,
        decoration: BoxDecoration(
          borderRadius: const BorderRadius.all(Radius.circular(90.0)),
          color: Colors.grey.withOpacity(0.4),
        ),
        child: Center(
          child: Text(smlPoliciesConfig.languageMap['deny'],
              style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold,
                  color: Colors.grey)
          ),
        ),
      ),
    );
  }

  void confirm() {
    if (ableToConfirm) {
      Navigator.pop(context);
    } else {
      smlPoliciesConfig.onToast(smlPoliciesConfig.languageMap['slideBottomToast']);
    }
  }

}
